import { ecs } from "db://oops-framework/libs/ecs/ECS";
import { Scene } from "../Scene";
import { RoleViewComp } from "../../../role/view/RoleViewComp";
import { smc } from "../../../common/ecs/SingletonModuleComp";
import { Role } from "../../../role/Role";
import { BattleType } from "../SceneEvent";
import { Skill } from "../../skill/Skill";

/**
 * 战斗前加载数据
 */
@ecs.register('LoadBattleData')
export class LoadBattleDataComp extends ecs.Comp {
    /** 1: 人跟怪，2: 人， 3：怪 */
    public loadType: number = 1;
    reset() {
        this.loadType = 1;
    }
}
export class LoadBattleDataSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
    filter(): ecs.IMatcher {
        return ecs.allOf(LoadBattleDataComp);
    }

    entityEnter(e: Scene): void {
        var mmr = e.MapModelRole;
        mmr.roles = [];

        if (e.LoadBattleData.loadType == 1) {
            this.loadRoles(e);
            this.loadEnemys(e);
        } else if (e.LoadBattleData.loadType == 2) {
            this.loadRoles(e);
        } else {
            this.loadEnemys(e);
        }
        mmr.roles = mmr.friends.concat(mmr.enemys);

        setTimeout(() => {
            e.loadBattleRoles(e.LoadBattleData.loadType);
            e.remove(LoadBattleDataComp);
        }, 200);
    }

    private loadRoles(e: Scene) {
        e.MapModelRole.friends.forEach(element => {
            element.remove(RoleViewComp);
        });
        e.MapModelRole.friends = [];
        var mmr = e.MapModelRole;
        smc.account.AccountModel.battles.forEach(element => {
            let role = ecs.getEntity<Role>(Role);
            role.RoleModel.id = element.RoleModel.id;
            role.RoleModel.uuid = element.RoleModel.uuid;
            role.RoleModel.pos = element.RoleModel.pos;
            role.RoleModel.stage = element.RoleModel.stage;
            role.RoleModel.hasOpen = element.RoleModel.hasOpen;
            role.RoleModel.battleType = BattleType.Scene;
            role.RoleModel.myTeam = true;
            role.setStar(element.RoleModel.star);
            role.setLevel(element.RoleModel.level);
            //设置默认技能
            this.setRoleSkill(role);
            mmr.friends.push(role);
        });
    };
    private loadEnemys(e: Scene) {
        e.MapModelRole.enemys.forEach(element => {
            element.remove(RoleViewComp);
        });
        e.MapModelRole.enemys = [];
        var bm = e.BattleModel;
        bm.id = e.BattleModel.daGuan;

        var monsters: any[] = bm.table.monsters[bm.xiaoguan - 1];
        if (!monsters) {
            console.warn("缺少怪物配置");
            return
        }

        var hasBoss: boolean = false;
        var bossName: string = null!;
        monsters.forEach(element => {
            var role = this.createEnemy(element);
            if (element[0] > 100) {
                //宝箱或者boss
                hasBoss = true;
                bossName = role.RoleModel.table.nickName;
            }
        });

        //有boss或者宝箱倒计时3分36秒
        if (hasBoss) {
            e.setBossLastTime(218, bossName);
        } else {
            e.setBossLastTime(-1, bossName);
        }
    };

    private createEnemy(list: any[]) {
        let role = ecs.getEntity<Role>(Role);
        role.RoleModel.id = list[0];
        role.RoleModel.uuid = list[1];
        role.RoleModel.pos = list[1];
        role.RoleModel.stage = 0;
        role.setStar(0);
        role.RoleModel.battleType = BattleType.Scene;
        role.setLevel(list[2]);
        role.RoleModel.myTeam = false;
        smc.scene.MapModelRole.enemys.push(role);

        //设置默认技能
        this.setRoleSkill(role);

        return role
    }
    private setRoleSkill(role: Role) {
        var table = role.RoleModel.table;
        if (table.normal) {
            var normalSkill = ecs.getEntity<Skill>(Skill);
            normalSkill.SkillModel.id = table.normal;
            normalSkill.learn(role);
            role.RoleModelSkill.normal = normalSkill;
        }

        if (table.skill) {
            var skillSkill = ecs.getEntity<Skill>(Skill);
            skillSkill.SkillModel.id = table.skill;
            skillSkill.learn(role);
            role.RoleModelSkill.power = skillSkill;
        }

        if (table.passives) {
            (table.passives as Array<number>).forEach(element => {
                let passivesSkill = ecs.getEntity<Skill>(Skill);
                passivesSkill.SkillModel.id = element;
                passivesSkill.learn(role);
                role.RoleModelSkill.passives.push(passivesSkill);
            });
        }

        if (table.skillAwake) {
            var skillSkillAwake = ecs.getEntity<Skill>(Skill);
            skillSkillAwake.SkillModel.id = table.skillAwake;
            skillSkillAwake.learn(role);
            role.RoleModelSkill.powerAwake = skillSkillAwake;
        }

        if (table.passivesAwake) {
            (table.passivesAwake as Array<number>).forEach(element => {
                let passivesSkillAwake = ecs.getEntity<Skill>(Skill);
                passivesSkillAwake.SkillModel.id = element;
                passivesSkillAwake.learn(role);
                role.RoleModelSkill.passivesAwake.push(passivesSkillAwake);
            });
        }

        // if (role.RoleModel.armQi) {
        //     (role.RoleModel.armQi.ArtifactModel.table.skill as Array<number>).forEach(id => {
        //         let qiSkill = ecs.getEntity<Skill>(Skill);
        //         qiSkill.SkillModel.id = id;
        //         qiSkill.SkillModel.learned = 1;
        //         role!.RoleModelSkill.qiMap.set(role.RoleModel.armQi.ArtifactModel.table.qi_id, qiSkill);
        //     });
        // }
    }
}